﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web.Mvc;

namespace Hasiu.Mvc.Controllers.Tools
{
    public class CutimgToolsController : Controller
    {
        public ActionResult Index()
        {
            try
            {
                if (Request["t"] != null)
                {
                    switch (Request["t"])
                    {
                        case "r":
                            if (Request["a"] != null && Request["s"] != null)
                            {
                                var src = Server.MapPath("~/" + Request["s"]);
                                var angle = float.Parse(Request["a"]);
                                var na = Math.Abs(angle);
                                if (na != 90 && na != 180)
                                {
                                    Response.Write("旋转角度不能为" + angle + "度!");
                                }
                                var oldImage = Image.FromFile(src);
                                var ow = oldImage.Width;
                                var oh = oldImage.Height;
                                var nw = ow;
                                var nh = oh;
                                if (na == 90)
                                {
                                    nw = oh;
                                    nh = ow;
                                }
                                var bmp = new Bitmap(nw, nh);
                                using (var g = Graphics.FromImage(bmp))
                                {
                                    g.InterpolationMode = InterpolationMode.Bilinear;
                                    g.SmoothingMode = SmoothingMode.HighQuality;

                                    var offset = new Point((nw - ow)/2, (nh - oh)/2);
                                    var rect = new Rectangle(offset.X, offset.Y, ow, oh);
                                    var center = new Point(rect.X + rect.Width/2, rect.Y + rect.Height/2);
                                    g.TranslateTransform(center.X, center.Y);
                                    g.RotateTransform(angle);
                                    g.TranslateTransform(-center.X, -center.Y);
                                    g.DrawImage(oldImage, rect);
                                    g.ResetTransform();
                                    g.Save();
                                }
                                oldImage.Dispose();
                                bmp.Save(src);
                                bmp.Dispose();
                                Response.Write("success");
                            }
                            break;
                        case "g":
                            {
                                double zoom = 1;
                                if (Request["z"] != null)
                                {
                                    zoom = double.Parse(Request["z"]);
                                }
                                if (Request["s"] != null && Request["x"] != null && Request["y"] != null && Request["w"] != null &&
                                    Request["h"] != null)
                                {
                                    var src = Server.MapPath("~/" + Request["s"]);
                                    var xx = int.Parse(Request["x"]);
                                    var yy = int.Parse(Request["y"]);
                                    var ww = int.Parse(Request["w"]);
                                    var hh = int.Parse(Request["h"]);
                                    var oldImage = new Bitmap(src);
                                    var newBitmap = new Bitmap((int) (zoom*oldImage.Width), (int) (zoom*oldImage.Height));
                                    using (var g = Graphics.FromImage(newBitmap))
                                    {
                                        g.InterpolationMode = InterpolationMode.High;
                                        g.SmoothingMode = SmoothingMode.HighQuality;
                                        g.SmoothingMode = SmoothingMode.AntiAlias;
                                        g.CompositingQuality = CompositingQuality.HighQuality;
                                        g.DrawImage(oldImage,
                                                    new Rectangle(0, 0, (int) (zoom*oldImage.Width),
                                                                  (int) (zoom*oldImage.Height)),
                                                    new Rectangle(0, 0, oldImage.Width, oldImage.Height), GraphicsUnit.Pixel);
                                        g.Save();
                                    }
                                    var bmp = new Bitmap(ww, hh);
                                    using (var g = Graphics.FromImage(bmp))
                                    {
                                        g.InterpolationMode = InterpolationMode.High;
                                        g.SmoothingMode = SmoothingMode.HighQuality;
                                        g.SmoothingMode = SmoothingMode.AntiAlias;
                                        g.CompositingQuality = CompositingQuality.HighQuality;
                                        g.DrawImage(newBitmap, 0, 0, new Rectangle(xx, yy, ww, hh), GraphicsUnit.Pixel);
                                        g.Save();
                                        newBitmap.Dispose();
                                    }
                                    if (Request["w2"] != null & Request["h2"] != null)
                                    {
                                        var towidth = int.Parse(Request["w2"]);
                                        var toheight = int.Parse(Request["h2"]);
                                        const int x = 0;
                                        const int y = 0;
                                        var ow = bmp.Width;
                                        var oh = bmp.Height;
                                        var bitmap = new Bitmap(towidth, toheight);
                                        var g = Graphics.FromImage(bitmap);
                                        g.InterpolationMode = InterpolationMode.High;
                                        g.SmoothingMode = SmoothingMode.HighQuality;
                                        g.Clear(Color.Transparent);
                                        g.DrawImage(bmp, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh),
                                                    GraphicsUnit.Pixel);
                                        oldImage.Dispose();
                                        bitmap.Save(src);
                                        bmp.Dispose();
                                        bitmap.Dispose();
                                        g.Dispose();
                                    }
                                    else
                                    {
                                        oldImage.Dispose();
                                        bmp.Save(src);
                                        bmp.Dispose();
                                    }
                                    Response.Write("success");
                                }
                            }
                            break;
                        case "t":
                            if (Request["s"] != null && Request["w"] != null && Request["h"] != null)
                            {
                                var src = Server.MapPath("~/" + Request["s"]);
                                var bmp = new Bitmap(src);
                                var towidth = int.Parse(Request["w"]);
                                var toheight = int.Parse(Request["h"]);
                                const int x = 0;
                                const int y = 0;
                                var ow = bmp.Width;
                                var oh = bmp.Height;
                                var bitmap = new Bitmap(towidth, toheight);
                                var g = Graphics.FromImage(bitmap);
                                g.InterpolationMode = InterpolationMode.High;
                                g.SmoothingMode = SmoothingMode.HighQuality;
                                g.Clear(Color.Transparent);
                                g.DrawImage(bmp, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh),
                                            GraphicsUnit.Pixel);
                                bmp.Dispose();
                                var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(src);
                                var src2 = Path.GetDirectoryName(src) + "/" + fileName;
                                bitmap.Save(src2);
                                var returnFileName = Path.GetDirectoryName(Request["s"]) + "/" + fileName;
                                returnFileName = returnFileName.Replace("\\", "/");
                                returnFileName = returnFileName.Replace("//", "/");
                                Response.Write(returnFileName);
                                bitmap.Dispose();
                                g.Dispose();
                            }
                            break;
                    }
                }
            }
            catch
            {
            }
            return new EmptyResult();
        }
    }
}